package com.xhwy.controller;

import com.xhwy.param.OrderParam;
import com.xhwy.service.AlipayService;
import com.xhwy.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/alipay")
@Slf4j
public class AliPayController {

    @Autowired
    private AlipayService alipayService;

    @PostMapping("/trade/page/pay")
    public R tradePay(@RequestBody @Validated OrderParam orderParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("参数异常，无法生成订单");
        }
        //支付宝开放平台接收 request请求对象后
        //会为开发者生成一个html 形式的form表单，包含自动提交的脚本
        String formStr = alipayService.tradePay(orderParam);
        //我们将form表单字符串返回给前端程序，之后前端将会调用自动提交脚本，进行表单的提交
        //此时表单会自动提交到action属性所指向的支付宝开放平台中，从而为用户展示一个支付页面
        return R.ok().put("data", formStr);
    }


}
